class Solution {
public:
    string addBinary(string a, string b) {
        int add = 0;
        int cur1 = a.size() - 1, cur2 = b.size() - 1;
        string ret;
        while (cur1 >= 0 || cur2 >= 0 || add)
        {
            int sum = add;
            if (cur1 >= 0)
                sum += (a[cur1--] - '0');
            if (cur2 >= 0)
                sum += (b[cur2--] - '0');

            ret += (sum % 2 + '0');
            add = sum / 2;
        }
        reverse(ret.begin(), ret.end());
        return ret;
    }
};